/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  TempReportReadActivity.java   
 * @Package com.jiyi.activity.new_maintenance   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-9-4 下午4:54:40   
 * @version V1.0     
 */
package com.jiyi.activity.new_maintenance;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.bean.maintenance.RepairTask.RepairTaskItem;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: TempReportReadActivity
 * @Description:TODO(查看临检报表)
 * @author: HYM
 * @date: 2017-9-4 下午4:54:40
 * 
 */
public class TempReportReadActivity extends ActivityBase {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.tvRepairFirstEquip)
	TextView tvRepairFirstEquip;
	@BindView(R.id.tvRepairSecondEquip)
	TextView tvRepairSecondEquip;
	@BindView(R.id.tvRepairEquipItem)
	TextView tvRepairEquipItem;
	@BindView(R.id.tvEquipNo)
	TextView tvEquipNo;
	@BindView(R.id.tvEquipPosition)
	TextView tvEquipPosition;
	@BindView(R.id.tvEquipRecord)
	TextView tvEquipRecord;
	@BindView(R.id.tvEquipName)
	TextView tvEquipName;
	@BindView(R.id.tvEquipBrand)
	TextView tvEquipBrand;
	@BindView(R.id.tvCreateDate)
	TextView tvCreateDate;
	@BindView(R.id.tvRemainDate)
	TextView tvRemainDate;
	@BindView(R.id.tvEquipModel)
	TextView tvEquipModel;
	@BindView(R.id.tvEquipStatus)
	TextView tvEquipStatus;
	@BindView(R.id.tvEquipPrice)
	TextView tvEquipPrice;
	@BindView(R.id.tvRepairPrice)
	TextView tvRepairPrice;
	@BindView(R.id.tvPayer)
	TextView tvPayer;
	@BindView(R.id.tvOwnerCheck)
	TextView tvOwnerCheck;
	@BindView(R.id.llProduct)
	LinearLayout llProduct;

	String bdid;
	int index;

	// 是否展开产品详情
	boolean isExtendProduct = false;
	
	Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_temp_report_read);
		ButterKnife.bind(this);
		init();
	}

	private void init() {
		getIntentData();
		setTop();
		getData();
	}
	
	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		super.onDestroy();
	}

	private void getIntentData() {
		bdid = getIntent().getStringExtra("bdid");
		index = getIntent().getIntExtra("index", 0);
	}

	private void setTop() {
		tvTitle.setText("临检报表" + index);
		tvSearch.setVisibility(View.GONE);
	}

	private void getData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", bdid);

		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, WebApi.MAINTENANCE_REPAIR_DETAIL,
				"", params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								Log.e("dfdsf   ", result);
								RepairTaskItem temp = new Gson().fromJson(data,
										RepairTaskItem.class);
								setData(temp);
							} 
							else if (status.equals("404") && info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg("尚未登录或该用户在其他地方登录，请重新登录！");
								//清除环信登录
								UserHelp.getInstance().clearUser(AppBase.Context, true);
								return;
							}
							else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}

	@OnClick({ R.id.tvBack, R.id.rlMoreProduct })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.rlMoreProduct:
			if (isExtendProduct) {
				llProduct.setVisibility(View.GONE);
				isExtendProduct = false;
			} else {
				llProduct.setVisibility(View.VISIBLE);
				isExtendProduct = true;
			}
			break;
		default:
			break;
		}
	}

	void setData(RepairTaskItem repairTaskItem) {
		tvRepairFirstEquip.setText(repairTaskItem.getInspection_name());
		tvRepairSecondEquip.setText(repairTaskItem.getEquipment_name());
		tvRepairEquipItem.setText(repairTaskItem.getBad_name());
		tvEquipNo.setText(repairTaskItem.getBh_code());
		tvEquipPosition.setText(repairTaskItem.getPlace());
		tvEquipRecord.setText(TextUtils.isEmpty(repairTaskItem.getComments())?repairTaskItem.getRecord():repairTaskItem.getComments());
		tvEquipName.setText(repairTaskItem.getPro_name());
		tvEquipBrand.setText(repairTaskItem.getProduct());
		tvCreateDate.setText(repairTaskItem.getPro_date());
		tvRemainDate.setText(repairTaskItem.getPro_safe_date());
		tvEquipModel.setText(repairTaskItem.getPro_type());
		String status = null;// 0新增，1维修，2更换,3保养，4保修
		if (repairTaskItem.getFlag() == 0) {
			status = "新增";
		} else if (repairTaskItem.getFlag() == 1) {
			status = "维修";
		} else if (repairTaskItem.getFlag() == 2) {
			status = "更换";
		} else if (repairTaskItem.getFlag() == 3) {
			status = "保养";
		} else if (repairTaskItem.getFlag() == 4) {
			status = "保修";
		}
		tvEquipStatus.setText(status);
		tvEquipPrice.setText(repairTaskItem.getPrice());
		tvRepairPrice.setText(repairTaskItem.getAmount());
		

		String check_status = null;// 0新增，1维修，2更换,3保养，4保修
		if (repairTaskItem.getAuditor_status() == 0) {
			check_status = "待审核";
		} else if (repairTaskItem.getAuditor_status() == 1) {
			check_status = "同意报修";
		} else if (repairTaskItem.getAuditor_status() == 2) {
			check_status = "不同意";
		}
		tvOwnerCheck.setText(check_status);
		
		if (repairTaskItem.getPayer() == 0) {
			//业主
			tvPayer.setText("业主方");
		}
		else {
			//维保单位
			tvPayer.setText("维保单位");
		}
	}
}
